home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UNIXTOOL / MEMACS / C / File < prev    next >
Text File  |  1990-06-26  |  22KB  |  895 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. /*
  16.  * Read a file into the current
  17.  * buffer. This is really easy; all you do is
  18.  * find the name of the file, and call the standard
  19.  * "read a file into the current buffer" code.
  20.  * Bound to "C-X C-R".
  21.  */
  22. PASCAL NEAR fileread(f, n)
  23.  
  24. int f, n;    /* defualt and numeric arguments (unused) */
  25.  
  26. {
  27.     register int s;    /* status return */
  28.     char *fname;    /* file name to read */
  29.  
  30.     if (restflag)        /* don't allow this command if restricted */
  31.         return(resterr());
  32.  
  33.     if ((fname = gtfilename(TEXT131)) == NULL)
  34. /*                              "Read file" */
  35.         return(FALSE);
  36.     return(readin(fname, TRUE));
  37. }
  38.  
  39. /*
  40.  * Insert a file into the current
  41.  * buffer. This is really easy; all you do it
  42.  * find the name of the file, and call the standard
  43.  * "insert a file into the current buffer" code.
  44.  * Bound to "C-X C-I".
  45.  */
  46. PASCAL NEAR insfile(f, n)
  47.  
  48. int f,n;    /* prefix flag and argument */
  49.  
  50. {
  51.     register int    s;
  52.     char *fname;    /* file name */
  53.     short int curoff;
  54.     LINE *curline;
  55.  
  56.     if (restflag)        /* don't allow this command if restricted */
  57.         return(resterr());
  58.     if (curbp->b_mode&MDVIEW)      /* don't allow this command if  */
  59.         return(rdonly());    /* we are in read only mode    */
  60.  
  61.     if ((fname = gtfilename(TEXT132)) == NULL) 
  62. /*                              "Insert file" */
  63.         return(FALSE);
  64.     /*
  65.      * Save the local pointers to hold global ".", in case
  66.      * $yankpnt is set to 1.
  67.      */
  68.     if (yanktype == SRBEGIN) {
  69.         /* Find the *previous* line, since the line we are on
  70.          * may disappear due to re-allocation.  This works even
  71.          * if we are on the first line of the file.
  72.          */
  73.         curline = lback(curwp->w_dotp);
  74.         curoff = curwp->w_doto;
  75.     }
  76.     s = ifile(fname);
  77.  
  78.     if (yanktype == SRBEGIN) {
  79.         curwp->w_dotp = lforw(curline);
  80.         curwp->w_doto = curoff;
  81.     }
  82.     return (s);
  83. }
  84.  
  85. /*
  86.  * Select a file for editing.
  87.  * Look around to see if you can find the
  88.  * fine in another buffer; if you can find it
  89.  * just switch to the buffer. If you cannot find
  90.  * the file, create a new buffer, read in the
  91.  * text, and switch to the new buffer.
  92.  * Bound to C-X C-F.
  93.  */
  94. PASCAL NEAR filefind(f, n)
  95.  
  96. int f,n;    /* prefix flag and argument */
  97.  
  98. {
  99.     char *fname;    /* file user wishes to find */    /* file name */
  100.     register int s;        /* status return */
  101.  
  102.     if (restflag)        /* don't allow this command if restricted */
  103.         return(resterr());
  104.  
  105.     if ((fname = gtfilename(TEXT133)) == NULL) 
  106. /*                              "Find file" */
  107.         return(FALSE);
  108.     return(getfile(fname, TRUE));
  109. }
  110.  
  111. PASCAL NEAR viewfile(f, n)    /* visit a file in VIEW mode */
  112.  
  113. int f,n;    /* prefix flag and argument */
  114.  
  115. {
  116.     char *fname;    /* file user wishes to find */    /* file name */
  117.     register int s;    /* status return */
  118.  
  119.     if (restflag)        /* don't allow this command if restricted */
  120.         return(resterr());
  121.  
  122.     if ((fname = gtfilename(TEXT134)) == NULL) 
  123. /*                              "View file" */
  124.         return(FALSE);
  125.     s = getfile(fname, FALSE);
  126.     if (s) {    /* if we succeed, put it in view mode */
  127.         curwp->w_bufp->b_mode |= MDVIEW;
  128.         upmode();
  129.     }
  130.     return(s);
  131. }
  132.  
  133. #if    CRYPT
  134. PASCAL NEAR resetkey()    /* reset the encryption key if needed */
  135.  
  136. {
  137.     register int s; /* return status */
  138.  
  139.     /* turn off the encryption flag */
  140.     cryptflag = FALSE;
  141.  
  142.     /* if we are in crypt mode */
  143.     if (curbp->b_mode & MDCRYPT) {
  144.         if (curbp->b_key[0] == 0) {
  145.             s = setekey(FALSE, 0);
  146.             if (s != TRUE)
  147.                 return(s);
  148.         }
  149.  
  150.         /* let others know... */
  151.         cryptflag = TRUE;
  152.  
  153.         /* and set up the key to be used! */
  154.         /* de-encrypt it */
  155.         crypt((char *)NULL, 0);
  156.         crypt(curbp->b_key, strlen(curbp->b_key));
  157.  
  158.         /* re-encrypt it...seeding it to start */
  159.         crypt((char *)NULL, 0);
  160.         crypt(curbp->b_key, strlen(curbp->b_key));
  161.     }
  162.  
  163.     return(TRUE);
  164. }
  165. #endif
  166.  
  167. PASCAL NEAR getfile(fname, lockfl)
  168.  
  169. char fname[];        /* file name to find */
  170. int lockfl;        /* check the file for locks? */
  171.  
  172. {
  173.     register BUFFER *bp;
  174.     register LINE    *lp;
  175.     register int    i;
  176.     register int    s;
  177.     register int cmark;    /* current mark */
  178.     char bname[NBUFN];    /* buffer name to put file */
  179.  
  180. #if    MSDOS | OS2 | AOSVS | VMS | ATARI
  181.     mklower(fname);            /* msdos isn't case sensitive */
  182. #endif
  183.     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  184. #if    RISCOS
  185.         /* Compare file names without worrying about case */
  186.         if ((bp->b_flag&BFINVS)==0 && strlcmp(bp->b_fname, fname)==0) {
  187. #else
  188.         if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  189. #endif
  190.             swbuffer(bp);
  191.             lp = curwp->w_dotp;
  192.             i = curwp->w_ntrows/2;
  193.             while (i-- && lback(lp)!=curbp->b_linep)
  194.                 lp = lback(lp);
  195.             curwp->w_linep = lp;
  196.             curwp->w_flag |= WFMODE|WFHARD;
  197.             mlwrite(TEXT135);
  198. /*                              "[Old buffer]" */
  199.             return(TRUE);
  200.         }
  201.     }
  202.     makename(bname, fname);         /* New buffer name.    */
  203.  
  204.     while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  205.         /* old buffer name conflict code */
  206.         s = mlreply(TEXT136, bname, NBUFN);
  207. /*                          "Buffer name: " */
  208.         if (s == ABORT)         /* ^G to just quit    */
  209.             return(s);
  210.         if (s == FALSE) {        /* CR to clobber it    */
  211.             makename(bname, fname);
  212.             break;
  213.         }
  214.     }
  215.     if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  216.         mlwrite(TEXT137);
  217. /*                      "Cannot create buffer" */
  218.         return(FALSE);
  219.     }
  220.  
  221.     if (--curbp->b_nwnd == 0) {        /* Undisplay.        */
  222.         curbp->b_dotp = curwp->w_dotp;
  223.         curbp->b_doto = curwp->w_doto;
  224.         for (cmark = 0; cmark < NMARKS; cmark++) {
  225.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  226.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  227.         }
  228.         curbp->b_fcol = curwp->w_fcol;
  229.     }
  230.     curbp = bp;                /* Switch to it.    */
  231.     curwp->w_bufp = bp;
  232.     curbp->b_nwnd++;
  233.     return(readin(fname, lockfl));        /* Read it in.        */
  234. }
  235.  
  236. /*
  237.     Read file "fname" into the current buffer, blowing away any text
  238.     found there.  Called by both the read and find commands.  Return
  239.     the final status of the read.  Also called by the mainline, to
  240.     read in a file specified on the command line as an argument. 
  241.     The command in $readhook is called after the buffer is set up
  242.     and before it is read. 
  243. */
  244.  
  245. PASCAL NEAR readin(fname, lockfl)
  246.  
  247. char    fname[];    /* name of file to read */
  248. int    lockfl;        /* check for file locks? */
  249.  
  250. {
  251.     register LINE *lp1;
  252.     register LINE *lp2;
  253.     register int i;
  254.     register WINDOW *wp;
  255.     register BUFFER *bp;
  256.     register int s;
  257.     register int nbytes;
  258.     register int nline;
  259.     register int cmark;    /* current mark */
  260.     char mesg[NSTRING];
  261.  
  262. #if    FILOCK
  263.     if (lockfl && lockchk(fname) == ABORT)
  264.         return(ABORT);
  265. #endif
  266.  
  267.     bp = curbp;                /* Cheap.        */
  268.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  269.         return(s);
  270.     bp->b_flag &= ~(BFINVS|BFCHG);
  271.     strcpy(bp->b_fname, fname);
  272. #if    FTYPE
  273.     /* Save the file type */
  274.     bp->b_type = fgettype(fname);
  275. #endif
  276.  
  277.     /* let a user macro get hold of things...if he wants */
  278.     execkey(&readhook, FALSE, 1);
  279.  
  280. #if    CRYPT
  281.     /* set up for decryption */
  282.     s = resetkey();
  283.     if (s != TRUE)
  284.         return(s);
  285. #endif
  286.  
  287.     /* turn off ALL keyboard translation in case we get a dos error */
  288.     TTkclose();
  289.  
  290.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  291.         goto out;
  292.  
  293.     if (s == FIOFNF) {            /* File not found.    */
  294.         mlwrite(TEXT138);
  295. /*                      "[New file]" */
  296.         goto out;
  297.     }
  298.  
  299.     /* read the file in */
  300.     mlwrite(TEXT139);
  301. /*              "[Reading file]" */
  302.     nline = 0;
  303.     while ((s=ffgetline()) == FIOSUC) {
  304.         nbytes = strlen(fline);
  305.         if ((lp1=lalloc(nbytes)) == NULL) {
  306.             s = FIOMEM;        /* Keep message on the    */
  307.             break;            /* display.        */
  308.         }
  309.         lp2 = lback(curbp->b_linep);
  310.         lp2->l_fp = lp1;
  311.         lp1->l_fp = curbp->b_linep;
  312.         lp1->l_bp = lp2;
  313.         curbp->b_linep->l_bp = lp1;
  314.         for (i=0; i<nbytes; ++i)
  315.             lputc(lp1, i, fline[i]);
  316.         ++nline;
  317.     }
  318.     ffclose();                /* Ignore errors.    */
  319.     strcpy(mesg, "[");
  320.     if (s==FIOERR) {
  321.         strcat(mesg, TEXT141);
  322. /*                           "I/O ERROR, " */
  323.         curbp->b_flag |= BFTRUNC;
  324.     }
  325.     if (s == FIOMEM) {
  326.         strcat(mesg, TEXT142);
  327. /*                           "OUT OF MEMORY, " */
  328.         curbp->b_flag |= BFTRUNC;
  329.     }
  330.     strcat(mesg, TEXT140);
  331. /*                   "Read " */
  332.     strcat(mesg, int_asc(nline));
  333.     strcat(mesg, TEXT143);
  334. /*                   " line" */
  335.     if (nline > 1)
  336.         strcat(mesg, "s");
  337.     strcat(mesg, "]");
  338.     mlwrite(mesg);
  339.  
  340. out:
  341.     TTkopen();    /* open the keyboard again */
  342.     for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  343.         if (wp->w_bufp == curbp) {
  344.             wp->w_linep = lforw(curbp->b_linep);
  345.             wp->w_dotp  = lforw(curbp->b_linep);
  346.             wp->w_doto  = 0;
  347.             for (cmark = 0; cmark < NMARKS; cmark++) {
  348.                 wp->w_markp[cmark] = NULL;
  349.                 wp->w_marko[cmark] = 0;
  350.             }
  351.             wp->w_flag |= WFMODE|WFHARD;
  352.         }
  353.     }
  354.     if (s == FIOERR || s == FIOFNF)     /* False if error.    */
  355.         return(FALSE);
  356.     return(TRUE);
  357. }
  358.  
  359. /*
  360.  * Take a file name, and from it
  361.  * fabricate a buffer name. This routine knows
  362.  * about the syntax of file names on the target system.
  363.  * I suppose that this information could be put in
  364.  * a better place than a line of code.
  365.  * Returns a pointer into fname indicating the end of the file path; i.e.,
  366.  * 1 character BEYOND the path name.
  367.  */
  368. char *PASCAL NEAR makename(bname, fname)
  369.  
  370. char *bname;
  371. char *fname;
  372.  
  373. {
  374.     register char *cp1;
  375.     register char *cp2;
  376.     register char *pathp;
  377.  
  378. #if     AOSVS | MV_UX
  379.         resolve_full_pathname(fname, fname);
  380.         mklower(fname);   /* aos/vs not case sensitive */
  381. #endif
  382.     cp1 = &fname[0];
  383.     while (*cp1 != 0)
  384.         ++cp1;
  385.  
  386. #if    AMIGA
  387.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  388.         --cp1;
  389. #endif
  390. #if     AOSVS | MV_UX
  391.         while (cp1!=&fname[0] && cp1[-1]!=':')
  392.                 --cp1;
  393. #endif
  394. #if    VMS
  395.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  396.         --cp1;
  397. #endif
  398. #if    MSDOS | OS2
  399.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  400.         --cp1;
  401. #endif
  402. #if    RISCOS
  403.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='.')
  404.         --cp1;
  405. #endif
  406. #if    ST520
  407.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  408.         --cp1;
  409. #endif
  410. #if    FINDER
  411.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  412.         --cp1;
  413. #endif
  414. #if    V7 | USG | SMOS | HPUX | BSD | SUN | XENIX | AVIION
  415.     while (cp1!=&fname[0] && cp1[-1]!='/')
  416.         --cp1;
  417. #endif
  418. #if WMCS
  419.     while (cp1!=&fname[0] && cp1[-1]!='_' && cp1[-1]!='/')
  420.         --cp1;
  421. #endif
  422.     /* cp1 is pointing to the first real filename char */
  423.     pathp = cp1;
  424.  
  425.     cp2 = &bname[0];
  426. #if RISCOS
  427.     while (cp2!=&bname[NBUFN-1] && *cp1!=0)
  428.         *cp2++ = *cp1++;
  429. #else
  430.     while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  431.         *cp2++ = *cp1++;
  432. #endif
  433.     *cp2 = 0;
  434.  
  435.     return(pathp);
  436. }
  437.  
  438. PASCAL NEAR unqname(name)    /* make sure a buffer name is unique */
  439.  
  440. char *name;    /* name to check on */
  441.  
  442. {
  443.     register char *sp;
  444.  
  445.     /* check to see if it is in the buffer list */
  446.     while (bfind(name, 0, FALSE) != NULL) {
  447.  
  448.         /* go to the end of the name */
  449.         sp = name;
  450.         while (*sp)
  451.             ++sp;
  452.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  453.             *sp++ = '0';
  454.             *sp = 0;
  455.         } else
  456.               *(--sp) += 1;
  457.     }
  458. }
  459.  
  460. /*
  461.  * Ask for a file name, and write the
  462.  * contents of the current buffer to that file.
  463.  * Update the remembered file name and clear the
  464.  * buffer changed flag. This handling of file names
  465.  * is different from the earlier versions, and
  466.  * is more compatable with Gosling EMACS than
  467.  * with ITS EMACS. Bound to "C-X C-W" for writing
  468.  * and ^X^A for appending.
  469.  */
  470.  
  471. PASCAL NEAR filewrite(f, n)
  472.  
  473. int f, n;    /* emacs arguments */
  474.  
  475. {
  476.     register int s;
  477.     char fname[NFILEN];
  478.  
  479.     if (restflag)        /* don't allow this command if restricted */
  480.         return(resterr());
  481.     if ((s=mlreply(TEXT144, fname, NFILEN)) != TRUE)
  482. /*                     "Write file: " */
  483.         return(s);
  484.     if ((s=writeout(fname, "w")) == TRUE) {
  485.         strcpy(curbp->b_fname, fname);
  486.         curbp->b_flag &= ~BFCHG;
  487.         /* Update mode lines.    */
  488.         upmode();
  489.     }
  490.     return(s);
  491. }
  492.  
  493. PASCAL NEAR fileapp(f, n)    /* append file */
  494.  
  495. int f, n;    /* emacs arguments */
  496.  
  497. {
  498.     register int s;
  499.     char fname[NFILEN];
  500.  
  501.     if (restflag)        /* don't allow this command if restricted */
  502.         return(resterr());
  503.     if ((s=mlreply(TEXT218, fname, NFILEN)) != TRUE)
  504. /*                     "Append file: " */
  505.         return(s);
  506.     if ((s=writeout(fname, "a")) == TRUE) {
  507.         curbp->b_flag &= ~BFCHG;
  508.         /* Update mode lines.    */
  509.         upmode();
  510.     }
  511.     return(s);
  512. }
  513.  
  514. /*
  515.  * Save the contents of the current
  516.  * buffer in its associatd file. Do nothing
  517.  * if nothing has changed (this may be a bug, not a
  518.  * feature). Error if there is no remembered file
  519.  * name for the buffer. Bound to "C-X C-S". May
  520.  * get called by "C-Z".
  521.  */
  522. PASCAL NEAR filesave(f, n)
  523.  
  524. int f,n;    /* prefix flag and argument */
  525.  
  526. {
  527.     register int s;
  528.  
  529.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  530.         return(rdonly());    /* we are in read only mode    */
  531.     if ((curbp->b_flag&BFCHG) == 0)     /* Return, no changes.    */
  532.         return(TRUE);
  533.     if (curbp->b_fname[0] == 0) {        /* Must have a name.    */
  534.         mlwrite(TEXT145);
  535. /*                      "No file name" */
  536.         return(FALSE);
  537.     }
  538.  
  539.     /* complain about truncated files */
  540.     if ((curbp->b_flag&BFTRUNC) != 0) {
  541.         if (mlyesno(TEXT146) == FALSE) {
  542. /*                          "Truncated file..write it out" */
  543.             mlwrite(TEXT8);
  544. /*                              "[Aborted]" */
  545.             return(FALSE);
  546.         }
  547.     }
  548.  
  549.     /* complain about narrowed buffers */
  550.     if ((curbp->b_flag&BFNAROW) != 0) {
  551.         if (mlyesno(TEXT147) == FALSE) {
  552. /*                          "Narrowed Buffer..write it out" */
  553.             mlwrite(TEXT8);
  554. /*                              "[Aborted]" */
  555.             return(FALSE);
  556.         }
  557.     }
  558.  
  559.     if ((s=writeout(curbp->b_fname, "w")) == TRUE) {
  560.         curbp->b_flag &= ~BFCHG;
  561.         /* Update mode lines.    */
  562.         upmode();
  563.     }
  564.     return(s);
  565. }
  566.  
  567. /*
  568.  * This function performs the details of file writing. It uses
  569.  * the file management routines in the "fileio.c" package. The
  570.  * number of lines written is displayed. Several errors are
  571.  * posible, and cause writeout to return a FALSE result. When
  572.  * $ssave is TRUE,  the buffer is written out to a temporary
  573.  * file, and then the old file is unlinked and the temporary
  574.  * renamed to the original name.  Before the file is written,
  575.  * a user specifyable routine (in $writehook) can be run.
  576.  */
  577.  
  578. PASCAL NEAR writeout(fn, mode)
  579.  
  580. char *fn;    /* name of file to write current buffer to */
  581. char *mode;    /* mode to open file (w = write a = append) */
  582. {
  583.     register LINE *lp;    /* line to scan while writing */
  584.     register char *sp;    /* temporary string pointer */
  585.     register int nline;    /* number of lines written */
  586.     int status;        /* return status */
  587.     int sflag;        /* are we safe saving? */
  588.     char tname[NSTRING];    /* temporary file name */
  589.     char buf[NSTRING];    /* message buffer */
  590.  
  591.     /* let a user macro get hold of things...if he wants */
  592.     execkey(&writehook, FALSE, 1);
  593.  
  594.     /* determine if we will use the save method */
  595.     sflag = FALSE;
  596.     if (ssave && fexist(fn) && *mode == 'w')
  597.         sflag = TRUE;
  598.  
  599. #if    CRYPT
  600.     /* set up for file encryption */
  601.     status = resetkey();
  602.     if (status != TRUE)
  603.         return(status);
  604. #endif
  605.  
  606.     /* turn off ALL keyboard translation in case we get a dos error */
  607.     TTkclose();
  608.  
  609.     /* Perform Safe Save..... */
  610.     if (sflag) {
  611.         /* duplicate original file name, and find where to trunc it */
  612.         sp = tname + (makename(tname, fn) - fn) + 1;
  613.         strcpy(tname, fn);
  614.  
  615.         /* create a unique name, using random numbers */
  616.         do {
  617. #if    ARM
  618.             /* Trim the result so that we have <10 characters */
  619.             strcpy(sp, int_asc(ernd() % 1000000));
  620. #else
  621.             *sp = 0;
  622.             strcat(tname, int_asc(ernd() & 0xffff));
  623. #endif
  624.         } while(fexist(tname));
  625.  
  626.         /* open the temporary file */
  627. #if     AOSVS
  628.                 status = ffwopen(fn, "w", tname);
  629. #else
  630.         status = ffwopen(tname, "w");
  631. #endif
  632.     } else
  633. #if     AOSVS
  634.                 status = ffwopen(fn, mode, NULL);
  635. #else
  636.         status = ffwopen(fn, mode);
  637. #endif
  638.  
  639.     /* if the open failed.. clean up and abort */
  640.     if (status != FIOSUC) {
  641.         TTkopen();
  642.         return(FALSE);
  643.     }
  644.  
  645.     /* write the current buffer's lines to the open disk file */
  646.     mlwrite(TEXT148);    /* tell us that we're writing */
  647. /*              "[Writing...]" */
  648.     lp = lforw(curbp->b_linep);    /* start at the first line.    */
  649.     nline = 0;            /* track the Number of lines    */
  650.     while (lp != curbp->b_linep) {
  651.         if ((status = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  652.             break;
  653.         ++nline;
  654.         lp = lforw(lp);
  655.     }
  656.  
  657.  
  658.     /* report on status of file write */
  659.     *buf = 0;
  660.     status |= ffclose();
  661.     if (status == FIOSUC) {
  662.         /* report on success (or lack therof) */
  663.         strcpy(buf, TEXT149);
  664. /*                          "[Wrote " */
  665.         strcat(buf, int_asc(nline));
  666.         strcat(buf, TEXT143);
  667. /*                          " line" */
  668.         if (nline > 1)
  669.             strcat(buf, "s");
  670.  
  671.         if (sflag) {
  672.             /* erase original file */
  673.             /* rename temporary file to original name */
  674.             if (unlink(fn) == 0 && rename(tname, fn) == 0)
  675.                 ;
  676.             else {
  677.                 strcat(buf, TEXT150);
  678. /*                                          ", saved as " */
  679.                 strcat(buf, tname);
  680.                 status = FIODEL;        /* failed */
  681.             }
  682.         }
  683.         strcat(buf, "]");
  684.         mlwrite(buf);
  685.     }
  686.  
  687. #if    FTYPE
  688.     /* set the file type */
  689.     if (*mode == 'w')
  690.         fsettype(fn,curbp->b_type);
  691. #endif
  692.     /* reopen the keyboard, and return our status */
  693.     TTkopen();
  694.     return(status == FIOSUC);
  695. }
  696.  
  697. /*
  698.  * The command allows the user
  699.  * to modify the file name associated with
  700.  * the current buffer. It is like the "f" command
  701.  * in UNIX "ed". The operation is simple; just zap
  702.  * the name in the BUFFER structure, and mark the windows
  703.  * as needing an update. You can type a blank line at the
  704.  * prompt if you wish.
  705.  */
  706.  
  707. PASCAL NEAR filename(f, n)
  708.  
  709. int f,n;    /* prefix flag and argument */
  710.  
  711. {
  712.     register int    s;
  713.     char        fname[NFILEN];
  714.  
  715.     if (restflag)        /* don't allow this command if restricted */
  716.         return(resterr());
  717.     if ((s=mlreply(TEXT151, fname, NFILEN)) == ABORT)
  718. /*                     "Name: " */
  719.         return(s);
  720.     if (s == FALSE)
  721.         strcpy(curbp->b_fname, "");
  722.     else
  723.         strcpy(curbp->b_fname, fname);
  724.     /* Update mode lines.    */
  725.     upmode();
  726.     curbp->b_mode &= ~MDVIEW;      /* no longer read only mode */
  727.     return(TRUE);
  728. }
  729.  
  730. /*
  731.  * Insert file "fname" into the current
  732.  * buffer, Called by insert file command. Return the final
  733.  * status of the read.
  734.  */
  735. PASCAL NEAR ifile(fname)
  736. char    fname[];
  737. {
  738.     register LINE *lp0;
  739.     register LINE *lp1;
  740.     register LINE *lp2;
  741.     register int i;
  742.     register BUFFER *bp;
  743.     register int s;
  744.     register int nbytes;
  745.     register int nline;
  746.     int cmark;    /* current mark */
  747.     char mesg[NSTRING];
  748.  
  749.     bp = curbp;                /* Cheap.        */
  750.     bp->b_flag |= BFCHG;            /* we have changed    */
  751.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  752.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  753.         goto out;
  754.     if (s == FIOFNF) {            /* File not found.    */
  755.         mlwrite(TEXT152);
  756. /*                      "[No such file]" */
  757.         return(FALSE);
  758.     }
  759.     mlwrite(TEXT153);
  760. /*              "[Inserting file]" */
  761.  
  762. #if    CRYPT
  763.     s = resetkey();
  764.     if (s != TRUE)
  765.         return(s);
  766. #endif
  767.     /* back up a line and save the mark here */
  768.     curwp->w_dotp = lback(curwp->w_dotp);
  769.     curwp->w_doto = 0;
  770.     for (cmark = 0; cmark < NMARKS; cmark++) {
  771.         curwp->w_markp[cmark] = curwp->w_dotp;
  772.         curwp->w_marko[cmark] = 0;
  773.     }
  774.  
  775.     nline = 0;
  776.     while ((s=ffgetline()) == FIOSUC) {
  777.         nbytes = strlen(fline);
  778.         if ((lp1=lalloc(nbytes)) == NULL) {
  779.             s = FIOMEM;        /* Keep message on the    */
  780.             break;            /* display.        */
  781.         }
  782.         lp0 = curwp->w_dotp;  /* line previous to insert */
  783.         lp2 = lp0->l_fp;    /* line after insert */
  784.  
  785.         /* re-link new line between lp0 and lp2 */
  786.         lp2->l_bp = lp1;
  787.         lp0->l_fp = lp1;
  788.         lp1->l_bp = lp0;
  789.         lp1->l_fp = lp2;
  790.  
  791.         /* and advance and write out the current line */
  792.         curwp->w_dotp = lp1;
  793.         for (i=0; i<nbytes; ++i)
  794.             lputc(lp1, i, fline[i]);
  795.         ++nline;
  796.     }
  797.     ffclose();                /* Ignore errors.    */
  798.     curwp->w_markp[0] = lforw(curwp->w_markp[0]);
  799.     strcpy(mesg, "[");
  800.     if (s==FIOERR) {
  801.         strcat(mesg, TEXT141);
  802. /*                           "I/O ERROR, " */
  803.         curbp->b_flag |= BFTRUNC;
  804.     }
  805.     if (s == FIOMEM) {
  806.         strcat(mesg, TEXT142);
  807. /*                           "OUT OF MEMORY, " */
  808.         curbp->b_flag |= BFTRUNC;
  809.     }
  810.     strcat(mesg, TEXT154);
  811. /*                   "Inserted " */
  812.     strcat(mesg, int_asc(nline));
  813.     strcat(mesg, TEXT143);
  814. /*                   " line" */
  815.     if (nline > 1)
  816.         strcat(mesg, "s");
  817.     strcat(mesg, "]");
  818.     mlwrite(mesg);
  819.  
  820. out:
  821.     /* advance to the next line and mark the window for changes */
  822.     curwp->w_dotp = lforw(curwp->w_dotp);
  823.     curwp->w_flag |= WFHARD | WFMODE;
  824.  
  825.     /* copy window parameters back to the buffer structure */
  826.     curbp->b_dotp = curwp->w_dotp;
  827.     curbp->b_doto = curwp->w_doto;
  828.     for (cmark = 0; cmark < NMARKS; cmark++) {
  829.         curbp->b_markp[cmark] = curwp->w_markp[cmark];
  830.         curbp->b_marko[cmark] = curwp->w_marko[cmark];
  831.     }
  832.     curbp->b_fcol = curwp->w_fcol;
  833.  
  834.     if (s == FIOERR)            /* False if error.    */
  835.         return(FALSE);
  836.     return(TRUE);
  837. }
  838.  
  839. /*    show-files    Bring up a fake buffer and list the
  840.             names of all the files in a given directory
  841. */
  842.  
  843. PASCAL NEAR showfiles(f, n)
  844.  
  845. int f,n;    /* prefix flag and argument */
  846.  
  847. {
  848.     register BUFFER *dirbuf;/* buffer to put file list into */
  849.     char outseq[NSTRING];    /* output buffer for file names */
  850.     char *sp;        /* output ptr for file names */
  851.     char mstring[NSTRING];    /* string to match cmd names to */
  852.     int status;        /* status return */
  853.  
  854.     /* ask what directory mask to search */
  855.     status = mlreply("Directory to show: ", mstring, NSTRING - 1);
  856.     if (status == ABORT)
  857.         return(status);
  858.  
  859.     /* get a buffer for the file list */
  860.     dirbuf = bfind("File List", TRUE, 0);
  861.     if (dirbuf == NULL || bclear(dirbuf) == FALSE) {
  862.         mlwrite("Can not display file list");
  863. /*            "Can not display function list" */
  864.         return(FALSE);
  865.     }
  866.  
  867.     /* let us know this is in progress */
  868.     mlwrite("[Building File List]");
  869.  
  870. #if RISCOS
  871.     /* Add a directory separator to the name */
  872.     strcat(mstring, DIRSEPSTR);
  873. #endif
  874.  
  875.     /* get the first file name */
  876.     sp = getffile(mstring);
  877.  
  878.     while (sp) {
  879.  
  880.         /* add a name to the buffer */
  881.         strcpy(outseq, sp);
  882.         if (addline(dirbuf, outseq) != TRUE)
  883.             return(FALSE);
  884.  
  885.         /* and get the next name */
  886.         sp = getnfile();
  887.     }
  888.  
  889.     /* display the list */
  890.     wpopup(dirbuf);
  891.     mlerase();    /* clear the mode line */
  892.     return(TRUE);
  893. }
  894.  
  895.